package com.larry.mr.filter;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * @ProjectName map-reduce
 * @Author larry
 * @Date 22:32 2022/4/7
 * @Description TODO
 **/
public class FRRecordWriter extends RecordWriter<Text, NullWritable> {

    FSDataOutputStream f1;
    FSDataOutputStream f2;

    public FRRecordWriter(TaskAttemptContext job) {

        try {
            FileSystem fs = FileSystem.get(job.getConfiguration());

            f1 = fs.create(new Path("C:\\Users\\larry\\IdeaProjects\\map-reduce\\src\\main\\resources\\output\\1.log"));
            f2 = fs.create(new Path("C:\\Users\\larry\\IdeaProjects\\map-reduce\\src\\main\\resources\\output\\2.log"));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void write(Text text, NullWritable nullWritable) throws IOException, InterruptedException {
        String s = text.toString();
        if (s.contains("atguigu")){
            f1.write(s.getBytes());
        }else {
            f2.write(s.getBytes());
        }
    }

    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        IOUtils.closeStream(f1);
        IOUtils.closeStream(f2);

    }
}
